package com.neu.api.controller.admin;


import com.neu.api.constan.StatusConst;
import com.neu.api.dto.CategoryDTO;
import com.neu.api.exception.ParamException;
import com.neu.api.query.CategoryQuery;
import com.neu.api.service.CategoryService;
import com.neu.api.service.impl.FoodServiceImpl;
import com.neu.api.utils.PageResult;
import com.neu.api.utils.Result;
import com.neu.api.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "分类接口")
@RestController
@RequestMapping("admin/category")
@Slf4j
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @Resource
    FoodServiceImpl foodService;

    @ApiOperation("查询分类接口")
    @GetMapping
    public Result<List<CategoryVO>> find(CategoryQuery categoryQuery){
        log.info("查询分类");
        PageResult<CategoryVO> page = categoryService.find(categoryQuery);
        return Result.ok("成功",page.getList(),page.getTotal());
    }

    /**
     * 添加分类信息
     * @param categoryDTO
     * @return
     */
    @ApiOperation("添加分类接口")
    @PostMapping
    public Result add(CategoryDTO categoryDTO){
        log.info("添加信息参数为："+categoryDTO.toString());
        categoryService.add(categoryDTO);
        return Result.ok("添加成功");
    }

    /**
     * 编辑分类信息
     * @param id
     * @param categoryDTO
     * @return
     */
    @ApiOperation("编辑分类")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id, CategoryDTO categoryDTO){
        log.info("编辑当前分类id为："+id+",参数为："+categoryDTO.toString());
        categoryService.update(id,categoryDTO);
        return Result.ok("修改成功！");
    }

    /**
     * 根据ID删除分类
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除分类")
    @DeleteMapping("/{id}")
    public Result deleteByID(@PathVariable long id){
        log.info("删除分类的id位："+id);

        categoryService.deleteById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 启用禁用账号
     * @param id
     * @return
     */
    @ApiOperation("启用禁用账号")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        log.info("启用禁用账号id:"+id+"，状态为："+status);
//        byte nowStatus = categoryService.updateStatus(id,status);
//        String msg = nowStatus==1||nowStatus==0?"账号禁用":"账号启用";
//        return Result.ok(msg);
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            categoryService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            categoryService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }
}
